package com.lxxie.test;

import com.lxxie.enums.Platform;
import com.lxxie.util.DESUtil;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Date 2021/11/1 15:20
 * @Created by xielx
 */
public class BatchUpdate {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = getHttpsClient();
        Map<String,String> map = new HashMap<>();
        CountDownLatch downLatch = new CountDownLatch(30);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(30,30,10, TimeUnit.MINUTES,new ArrayBlockingQueue<>(120));
        //String sql = "update net_rural_unit t set t.isdel=0 where dw_code='000510031015003018'";
        String sql = "insert into net_rural_code (LB, LBNAME, LBID, NO, MC, STATS, F_ID, ISMUST, CANEDIT, SFDM, BM, CSBJ, GXSJ, ISDEL, DELTIME, CS_CSBJ, CS_GXSJ, CS_BBH, CS_BZ, BZ)\n" +
            "values ('UserType', '用户类型', 999, '90', '系统管理员', 1, 0, 0, 0, '5100', '500019060100000053', '0', to_date('13-10-2021 09:53:44', 'dd-mm-yyyy hh24:mi:ss'), 0, null, '0', to_date('13-10-2021 09:53:44', 'dd-mm-yyyy hh24:mi:ss'), null, null, null)";
        String encodeSql = new String(Base64.encodeBase64(DESUtil.encrypt(sql.getBytes())));
        List<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("type","lady"));
        params.add(new BasicNameValuePair("data",encodeSql));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
        for (Platform sfdm : Platform.values()) {
            if (sfdm != Platform.SFDM_5000) {
                downLatch.countDown();
                continue;
            }
            pool.execute(()->{
                try{
                    HttpPost post = new HttpPost(sfdm.getUrl()+"/risinfo/system");
                    post.setEntity(entity);
                    CloseableHttpResponse resp = httpClient.execute(post);
                    String content = EntityUtils.toString(resp.getEntity());
                    System.out.println(content);
                    map.put(sfdm.getSfmc(),content);
                }catch (Exception e){
                   e.printStackTrace();
                   map.put(sfdm.getSfmc(),e.getMessage());
                }finally {
                    downLatch.countDown();
                }
            });
        }
        downLatch.await(6,TimeUnit.MINUTES);
        Arrays.stream(Platform.values()).forEach(temp-> System.out.println(temp.getSfmc()+":"+map.get(temp.getSfmc())));
    }

    private static CloseableHttpClient getHttpsClient() {
        CloseableHttpClient httpClient;
        SSLContext sslContext = null;
        try{
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
        }catch (Exception e){
            e.printStackTrace();
        }
        httpClient = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
        return httpClient;
    }
}
